package com.qf.huigou.controller;


import com.qf.huigou.Service.PhoneService;
import com.qf.huigou.dto.Assess;
import com.qf.huigou.entity.Brand;
import com.qf.huigou.entity.Page;
import com.qf.huigou.entity.PageInfo;
import com.qf.huigou.entity.Phone;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

@Controller
public class PhoneController {

    @Resource
    PhoneService phoneService;



    @RequestMapping("selectBrandNames")
    @ResponseBody
    public List<String> selectBrandNames(int status){
        List<String> brands = phoneService.selectBrandNames(status);
        return brands;
    }


    @RequestMapping("selectByPage")
    public String selectByPage(Model model,@RequestParam(value = "pageNo",required = false) String pageNo,@RequestParam(value = "phone_name",required = false)String phone_name,@RequestParam(value = "brand_name",required = false)String brand_name) throws UnsupportedEncodingException {
        int no = 1;
        if(pageNo!=null){
             no = Integer.parseInt(pageNo);
        }
        if(phone_name!=null){
            phone_name = new String(phone_name.getBytes("iso8859-1"),"utf-8");
        }
        if(brand_name!=null){
            brand_name = new String(brand_name.getBytes("iso8859-1"),"utf-8");
        }
        Page<Phone> phones = phoneService.selectByPage(no,phone_name,brand_name);
        model.addAttribute("phones",phones);
        return "huigou1";
    }

    /*@RequestMapping("selectByName")
    @ResponseBody
    public Page<Phone> selectByName(Model model,@RequestParam("name") String name){
        List<Phone> phones = phoneService.selectByName(name);
        Page<Phone> page = new Page<>();
        page.setData(phones);
        page.setPageCounts(1);
        page.setCurrentPage(1);
        *//*model.addAttribute("phones",page);*//*
        return page;
    }*/
    @RequestMapping("selectAssessInfo")
    public String selectAssessInfo(Model model,@RequestParam("id") int id){
        PageInfo pageInfo = phoneService.selectAssessInfo(id);
        model.addAttribute("pageInfo",pageInfo);
        return "huigou2";
    }

    @RequestMapping("toHuigou3")
    public String toHuigou3(Model model, @RequestParam("img") String img, @RequestParam("finalprice") String finalprice){
        List<String> list = new ArrayList<String>();
        list.add(img);
        list.add(finalprice);
        model.addAttribute("data",list);
        return "huigou3";
    }
}
